package com.originskyseed.platform.cms.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.CmsProductInfoRelateTask;
import com.originskyseed.platform.cms.service.ICmsProductInfoRelateTaskService;
import com.originskyseed.platform.cms.vo.request.CmsProductInfoRelateTaskRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;

/**
 * 产品关联任务表Controller类（OSI_PRODUCT_INFO_RELATE_TASK）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/cmsProductInfoRelateTask")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class CmsProductInfoRelateTaskController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CmsProductInfoRelateTaskController.class);

	@DubboReference
	private ICmsProductInfoRelateTaskService cmsProductInfoRelateTaskService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsProductInfoRelateTaskList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<CmsProductInfoRelateTask>> cmsProductInfoRelateTaskList(HttpServletRequest request) {
		BaseResponseBody<BasePage<CmsProductInfoRelateTask>> result = null;
		try {
			// 请求对象
			CmsProductInfoRelateTaskRequestVO requestVO = this.getParameterBean(CmsProductInfoRelateTaskRequestVO.class);
			requestVO.setIsDel(2);
			// 分页查询
			BasePage<CmsProductInfoRelateTask> page = new BasePage<CmsProductInfoRelateTask>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.cmsProductInfoRelateTaskService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<CmsProductInfoRelateTask>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_PRODUCT_TASK_LIST_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<CmsProductInfoRelateTask>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_PRODUCT_TASK_LIST_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsProductInfoRelateTaskSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<CmsProductInfoRelateTask> cmsProductInfoRelateTaskSave(HttpServletRequest request) {
		BaseResponseBody<CmsProductInfoRelateTask> result = null;
		try {
			// 请求对象
			CmsProductInfoRelateTask cmsProductInfoRelateTask = this.getParameterBean(CmsProductInfoRelateTask.class);

			if (StringUtils.isNotBlank(cmsProductInfoRelateTask.getId())) {
				this.cmsProductInfoRelateTaskService.updateByPrimaryKeySelective(cmsProductInfoRelateTask);
			} else {
				cmsProductInfoRelateTask.setIsDel(2);
				this.cmsProductInfoRelateTaskService.insertSelective(cmsProductInfoRelateTask);
			}
			result = new BaseResponseBody<CmsProductInfoRelateTask>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_PRODUCT_INFO_RELATE_TASK_SAVE_SUCCESS.getMsg()), cmsProductInfoRelateTask);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<CmsProductInfoRelateTask>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_PRODUCT_INFO_RELATE_TASK_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cmsProductInfoRelateTaskDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> cmsProductInfoRelateTaskDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.cmsProductInfoRelateTaskService.deleteByProductIds(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询产品信息详情
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectDetailsById.json")
	@RequiredParams(names = { "id" })
	public BaseResponseBody<CmsProductInfoRelateTask> selectDetailsById(HttpServletRequest request) {
		BaseResponseBody<CmsProductInfoRelateTask> result = null;
		try {
			// 请求对象
			String id = this.getParameterString("id", null);

			CmsProductInfoRelateTask cmsProductInfoRelateTask = new CmsProductInfoRelateTask();
			CmsProductInfoRelateTask tempCmsProductInfoRelateTask = new CmsProductInfoRelateTask();
			tempCmsProductInfoRelateTask.setProductId(id);
			tempCmsProductInfoRelateTask.setIsDel(2);
			List<CmsProductInfoRelateTask> list = this.cmsProductInfoRelateTaskService.selectList(tempCmsProductInfoRelateTask);
			if (list != null && list.size() > 0) {
				cmsProductInfoRelateTask = list.get(0);
			} else {
				return new BaseResponseBody<CmsProductInfoRelateTask>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CMS_PRODUCT_TASK_LIST_ISNULL.getMsg()), cmsProductInfoRelateTask);
			}
			result = new BaseResponseBody<CmsProductInfoRelateTask>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), cmsProductInfoRelateTask);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<CmsProductInfoRelateTask>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
